﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GlobalMBA
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                ShowUsage();
                return;
            }

            try
            {
                DoJob(args.First());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private static void DoJob(string path)
        {
            var list = GetFiles(path, "*.php,*.js,*.config,*.xml,*.htm,*.html,*.sql,*.txt,*.css");
            foreach (var f in list)
            {
                Console.WriteLine("writing {0}", f);
                var text = File.ReadAllText(f);
                var buffer = Encoding.UTF8.GetBytes(text);
                File.WriteAllBytes(f, buffer);
            }
            Console.WriteLine("{0} files were modified.", list.Length);
        }

        private static void ShowUsage()
        {
            Console.WriteLine("mba [path]");
        }

        private static string[] GetFiles(string path, string filter)
        {
            var s = new List<String>();
            foreach (var pattern in filter.Split(','))
            {
                s.AddRange(Directory.GetFiles(path, pattern, SearchOption.AllDirectories));
            }
            return s.ToArray();
        }
    }
}
